<?php
class EntriesController extends AppController {

	public $name = 'Entries';
	public $helpers = array('xml');
	public $components = array('RequestHandler');
	public $layout = 'xml/default';
	
	function beforeRender()
	{
		$this->disableCache();
	}
	
	private function __processTitlesOnly(&$entries)
	{
		if (key_exists('titlesonly', $this->params['form']) && $this->params['form']['titlesonly'] == "true")
		{
			foreach ($entries as $key => $value)
			{
				$entries[$key]['Entry']['value'] = '';
			}
		}
	}
	function index()
	{
		
		$entries = $this->Entry->findAll();
		$this->__processTitlesOnly($entries);
		$this->set('entries', array('entries' => $entries)); 
		// 'index' should be be specified because index() may be called from another methods
		$this->render('index');
	}
	function search()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$entries = array();
		App::import('Sanitize');
		$query = Sanitize::escape($this->params['form']['query']);
		
		if (strpos($query, "id=") === 0)
		{
			$ids = split(",", substr($query, 3));
			foreach ($ids as $id)
			{
				if (is_numeric($id))
				{
					$entry = $this->Entry->findById($id);
					if ($entry == array())
					{
						$this->cakeError("noSuchEntry", array('id' => $id));
					}
					$entries[] = $entry;
				}
				else
				{
					$this->cakeError("noSuchEntry", array('id' => $id)); 
				}
			}
		}
		else
		{
			// Had some strange problems with fulltext search, so using just LIKE 
			$conditions = array("or" => array (
								"Entry.value LIKE" => "%" . $query . "%", 
								"Entry.title LIKE" => "%" . $query . "%"));
			$entries = $this->Entry->find('all', array('conditions' => $conditions));
			/*	
			$conditions = array('MATCH (Entry.value) AGAINST("' . $query . '")');		
			$this->set('entries', array('entries' => 
							$this->Entry->find('all', array('conditions' => $conditions))));
			*/
		}

		$this->__processTitlesOnly($entries);
		$this->set('entries', array('entries' => $entries));
		$this->render('index');
	}
	function delete()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$this->Entry->delete($this->params['form']['id']);
		$this->index();
	}
	function add()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$this->Entry->create();
		$this->Entry->set("value", $this->params['form']['value']);
		$this->Entry->set("title", $this->params['form']['title']);
		if ($this->Entry->validates())
		{
			$this->Entry->save();
			$this->index();
		}
		else
		{
			$this->cakeError('notValidates');
		}
	}
	function edit()
	{
		if (!$this->RequestHandler->isPost())
		{
			return;
		}
		$this->Entry->read(null, $this->params['form']['id']);
		$this->Entry->set("value", $this->params['form']['value']);
		$this->Entry->set("title", $this->params['form']['title']);
		if ($this->Entry->validates())
		{
			$this->Entry->save();
			$this->index();
		}
		else
		{
			$this->cakeError('notValidates');
		}
	}
	function getpdf()
	{
		$data = array();
		$ids = "";
		if (key_exists('ids', $this->params['url']) && $this->params['url']['ids'])
		{
			$ids = $this->params['url']['ids'];
		}
		$data = $this->Entry->getEntriesByIds($ids);
		$this->set('data', $data);
		$this->layout = 'pdf';
		// The rest of the pdf generation code is in app/views/entries/getpdf.ctp
		$this->render();
	}
	function gethtml()
	{
		$data = array();
		$ids = "";
		if (key_exists('ids', $this->params['url']) && $this->params['url']['ids'])
		{
			$ids = $this->params['url']['ids'];
		}
		$data = $this->Entry->getEntriesByIds($ids);
		$this->set('data', $data);
		$this->set('ids', $ids);
		$this->layout = 'default';
		$this->render();	
	}
}
?>
